package com.chinairi.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chinairi.common.annotation.DoPage;
import com.chinairi.mall.config.ValidationResult;
import com.chinairi.mall.config.ValidatorImpl;
import com.chinairi.mall.constant.CiccTableNameEnum;
import com.chinairi.mall.controller.viewobject.ApproveDbIdVO;
import com.chinairi.mall.controller.viewobject.FourNewGoodsVO;
import com.chinairi.mall.dao.GoodsFeatureValueMapper;
import com.chinairi.mall.dao.GoodsPictureMapper;
import com.chinairi.mall.error.BusinessException;
import com.chinairi.mall.error.EmBusinessError;
import com.chinairi.mall.model.*;
import com.chinairi.mall.dao.FourNewGoodsMapper;
import com.chinairi.mall.service.IFourNewGoodsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chinairi.mall.util.CiccRecordUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * <p>
 * 四新商品表 服务实现类
 * </p>
 *
 * @author gsl
 * @since 2020-09-13
 */
@Service
public class FourNewGoodsServiceImpl extends ServiceImpl<FourNewGoodsMapper, FourNewGoods> implements IFourNewGoodsService {

    @Autowired
    private FourNewGoodsMapper fourNewGoodsMapper;

    @Autowired
    private ValidatorImpl validator;

    @Autowired
    private GoodsInfoServiceImpl goodsInfoServiceImpl;

    @Autowired
    private GoodsFeatureValueMapper goodsFeatureValueMapper;

    @Autowired
    private GoodsPictureMapper goodsPictureMapper;


    /**
     * 新增商品
     *
     * @param fourNewGoodsVO
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(FourNewGoodsVO fourNewGoodsVO) {
        ValidationResult result = validator.validate(fourNewGoodsVO);
        if (result.isHashErrors()) {
            throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR, result.getErrMsg());
        }
        String fngDbid = UUID.randomUUID().toString().replace("-", "");
        fourNewGoodsVO.setFngDbid(fngDbid);
        //设置默认状态，0未上架
        fourNewGoodsVO.setFngShelfState(0);
        fourNewGoodsVO.setApproveStatus(0);
        FourNewGoods fourNewGoods = new FourNewGoodsVO();
        BeanUtils.copyProperties(fourNewGoodsVO, fourNewGoods);
        fourNewGoodsMapper.insert(fourNewGoodsVO);
        goodsInfoServiceImpl.batchInsertFeature(fourNewGoodsVO.getGoodsFeatureValueList(), fngDbid);
        goodsInfoServiceImpl.batchInsertPic(fourNewGoodsVO.getGoodsPictureList(), fngDbid);
    }

    /**
     * 获取商品详细信息
     *
     * @param csDbid
     * @return
     */
    @Override
    @DoPage
    public List<FourNewGoods> getByCsDbId(FourNewGoodsVO fourNewGoodsVO, String csDbid) {
        QueryWrapper<FourNewGoods> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .like(StringUtils.isNotEmpty(fourNewGoodsVO.getFngName()), "FNG_NAME", fourNewGoodsVO.getFngName())
                .eq(fourNewGoodsVO.getFngShelfState() != null, "FNG_SHELF_STATE", fourNewGoodsVO.getFngShelfState())
                .eq(fourNewGoodsVO.getApproveStatus() != null, "APPROVE_STATUS", fourNewGoodsVO.getApproveStatus())
                .eq(StringUtils.isNotEmpty(csDbid), "CS_DBID", csDbid);
        List<FourNewGoods> list = this.list(queryWrapper);

        if (list !=null &&!list.isEmpty()){
            list.forEach(goodsInfo -> {
                CiccRecord ciccRecord = CiccRecordUtil.getCiccRecord(CiccTableNameEnum.GOODS_STATUS.getCodeValue(),String.valueOf(goodsInfo.getApproveStatus()));
                goodsInfo.setFngTypeLabel(CiccRecordUtil.getCiccRecord(CiccTableNameEnum.PRODUCT_TYPE.getCodeValue(),String.valueOf(goodsInfo.getFngType())).getLabel());
                goodsInfo.setApproveStatusLabel(ciccRecord.getLabel());
            });
        }
        return list;
    }

    /**
     * 更新商品
     *
     * @param fourNewGoodsVO
     */
    @Override
    public void updateGoods(FourNewGoodsVO fourNewGoodsVO) {
        ValidationResult result = validator.validate(fourNewGoodsVO);
        if (result.isHashErrors()) {
            throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR, result.getErrMsg());
        }
        FourNewGoods fourNewGoods = new FourNewGoodsVO();
        BeanUtils.copyProperties(fourNewGoodsVO, fourNewGoods);
        fourNewGoods.setFngShelfState(0);
        fourNewGoods.setApproveStatus(0);
        fourNewGoodsMapper.updateById(fourNewGoodsVO);
        goodsInfoServiceImpl.batchDelete(fourNewGoodsVO.getFngDbid());
        goodsInfoServiceImpl.batchInsertFeature(fourNewGoodsVO.getGoodsFeatureValueList(), fourNewGoodsVO.getFngDbid());
        goodsInfoServiceImpl.batchInsertPic(fourNewGoodsVO.getGoodsPictureList(), fourNewGoodsVO.getFngDbid());
    }

    /**
     * 获取商品特征值和图片
     *
     * @param fngDbid 四新商品id
     * @return
     */
    @Override
    public FourNewGoodsVO getFeatureAndPic(String fngDbid) {
        FourNewGoodsVO fourNewGoodsVO = new FourNewGoodsVO();
        Map<String, Object> map = new HashMap<>(16);
        map.put("GOODS_DBID", fngDbid);
        fourNewGoodsVO.setGoodsFeatureValueList(goodsFeatureValueMapper.selectByMap(map));
        fourNewGoodsVO.setGoodsPictureList(goodsPictureMapper.selectByMap(map));
        return fourNewGoodsVO;
    }

    /**
     * 批量提交商品审核
     *
     * @param approveDbIdVoList
     */
    @Override
    public void updateStatus(List<ApproveDbIdVO> approveDbIdVoList) {
        //获取前端传来的特征值对象
        if (approveDbIdVoList.size() > 0) {
            List<FourNewGoods> fourNewGoodsList = new ArrayList<>();
            for (ApproveDbIdVO approveDbIdVo : approveDbIdVoList) {
                FourNewGoods fourNewGoods = new FourNewGoods();
                fourNewGoods.setFngDbid(approveDbIdVo.getDbId());
                if (approveDbIdVo.getType() == 1) {
                    fourNewGoods.setFngShelfState(1);
                } else if (approveDbIdVo.getType() == 3) {
                    fourNewGoods.setFngShelfState(3);
                }
                fourNewGoods.setApproveStatus(3);

                fourNewGoodsList.add(fourNewGoods);
            }
            //批量插入特征值
            fourNewGoodsMapper.batchUpdateStatus(fourNewGoodsList);
        }
    }

    /**
     * 根据店铺ID获取四新商品列表
     *
     * @param fourNewGoods
     * @return
     */
    @Override
    public List<FourNewGoods> getListByCsDbId(FourNewGoods fourNewGoods) {
        return fourNewGoodsMapper.getListByCsDbId(fourNewGoods);
    }

}
